package com.study.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * cacheManager使用
     */
    @RequestMapping("/getString")
    @Cacheable(value = "springboot", key = "#root.methodName")
    //value标记一块缓存的区域　函数名作为key
    public String getString() {
        String str = "hello world";
        System.out.println("hahaha");
        return str;
    }

    /**
     * RedisTemplate & StringRedisTemplate
     */
    @RequestMapping("/set")
    public void set() {
        redisTemplate.opsForValue().set("myname", "xy");
        stringRedisTemplate.opsForValue().set("youname", "sb");
    }

    @RequestMapping("/get")
    public void get() {
        String s1 = (String) redisTemplate.opsForValue().get("myname");
        String s2 = stringRedisTemplate.opsForValue().get("youname");

        System.out.println(s1);
        System.out.println(s2);
    }
}
